package com.example.bilibili_fake.data.repository

import com.example.bilibili_fake.data.model.Archive
import com.example.bilibili_fake.data.network.ApiClient
import com.example.bilibili_fake.data.network.VideoApi
import com.example.bilibili_fake.utils.LogUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class VideoRepository {
    private val apiService: VideoApi = ApiClient.videoApiService

    suspend fun fetchPopularVideos(pageSize: Int = 8, pageNumber: Int = 1): Result<List<Archive>> {
        return withContext(Dispatchers.IO) {
            try {
                val response = apiService.getPopularVideos(pageSize, pageNumber,1)
                if (response.isSuccessful && response.body()?.code == 0) {
                    val archives = response.body()?.data?.archives ?: emptyList()
                    Result.success(archives)
                } else {
                    LogUtil.d("VideoRepository", "Failed to load popular videos: ${response.message()}")
                    Result.failure(Exception("Failed to load popular videos: ${response.message()}"))
                }
            } catch (e: Exception) {
                Result.failure(e)
            }
        }
    }
}